﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Models;
using System.Linq.Expressions;
using SBACN.WebSite.Code;

namespace SBACN.WebSite.MonthlyEvent
{
	public class Monthly
	{
		public string D0 { get; set; }
		public string D1 { get; set; }
		public string D2 { get; set; }
		public string D3 { get; set; }
		public string D4 { get; set; }
		public string D5 { get; set; }
		public string D6 { get; set; }
	}

	public partial class MonthlyEvent : MemberBasePage
	{
		protected string Dl { get; set; }
		protected string Dr { get; set; }
		protected string YearSelectDisplay { get; set; }
		protected void Page_Load(object sender, EventArgs e)
		{
			string DT = Request.QueryString["d"];

			DateTime N;
			if (!string.IsNullOrWhiteSpace(DT) && DateTime.TryParse(DT, out N))
			{
				N = Convert.ToDateTime(DT);
			}
			else
			{
				N = DateTime.Now;
			}
			YearSel_Display(N);
			this.Month_Sel.SelectedIndex = this.Month_Sel.Items.IndexOf(this.Month_Sel.Items.FindByValue(N.ToString("MM")));
			NowDate(N);
			BindData(N);
		}

		protected void NowDate(DateTime N)
		{
			this.Dl = N.AddMonths(-1).ToString("yyyy/MM");
			this.Dr = N.AddMonths(+1).ToString("yyyy/MM");
		}

		protected void BindData(DateTime N)
		{
			List<Monthly> Event = new List<Monthly>();

			int Days = DateTime.DaysInMonth(N.Year, N.Month);

			DateTime StartDate = Convert.ToDateTime(N.Year.ToString() + "/" + N.Month.ToString() + "/1");
			DateTime EndDate = Convert.ToDateTime(N.Year.ToString() + "/" + N.Month.ToString() + "/" + Days.ToString());
			string SD0 = "", SD1 = "", SD2 = "", SD3 = "", SD4 = "", SD5 = "", SD6 = "";

			while (EndDate >= StartDate)
			{
				int wn = Convert.ToInt32(StartDate.DayOfWeek);
				if (wn == 0)
				{
					if (Convert.ToDateTime(N.Year.ToString() + "/" + N.Month.ToString() + "/1") < StartDate)
						Event.Add(new Monthly { D0 = SD0, D1 = SD1, D2 = SD2, D3 = SD3, D4 = SD4, D5 = SD5, D6 = SD6 });

					SD0 = "";
					SD1 = "";
					SD2 = "";
					SD3 = "";
					SD4 = "";
					SD5 = "";
					SD6 = "";
				}
				switch (wn)
				{
					case 0:
						SD0 = StartDate.ToString();
						break;
					case 1:
						SD1 = StartDate.ToString();
						break;
					case 2:
						SD2 = StartDate.ToString();
						break;
					case 3:
						SD3 = StartDate.ToString();
						break;
					case 4:
						SD4 = StartDate.ToString();
						break;
					case 5:
						SD5 = StartDate.ToString();
						break;
					case 6:
						SD6 = StartDate.ToString();
						break;
				}

				if (StartDate == EndDate)
					Event.Add(new Monthly { D0 = SD0, D1 = SD1, D2 = SD2, D3 = SD3, D4 = SD4, D5 = SD5, D6 = SD6 });

				StartDate = StartDate.AddDays(1);
			}

			this.Rep.DataSource = Event;
			this.Rep.DataBind();

			//

		}

		protected string DateStr(string D)
		{
			if (string.IsNullOrWhiteSpace(D))
			{
				return "";
			}
			else
			{
				string S = Convert.ToDateTime(D).Month.ToString() + "月" + Convert.ToDateTime(D).Day.ToString() + "日";
				return S;
			}

		}
		protected string Color(string D)
		{
			if (string.IsNullOrWhiteSpace(D))
			{
				return "event_nodisplay";
			}
			else
			{
				return "event_display";
			}
		}

		protected string EventS(string D)
		{
			// 没有日期的格子直接跳出
			if (string.IsNullOrWhiteSpace(D))
				return "";

			int DisplayEventNum = 2;//一天显示事件数目
			int CutStringLen = 25;

			DateTime DT = Convert.ToDateTime(D);
			Expression<Func<TB_MonthlyEvents, bool>> f = (p => (p.EventDate == DT));
			Expression<Func<TB_MonthlyEvents, string>> order = (c => (c.StartHour));
			var result = DAL.DALHelper.Instance.GetList<TB_MonthlyEvents, string>(f, order, true);

			string s = "";
			string name = "";
			switch (result.Count)
			{
				case 0:
					break;
				default:
					int i = 0;
					foreach (var l in result)
					{
						if (i >= DisplayEventNum)
							break;

						if (l.Description.Length > CutStringLen)
							name = l.Description.Substring(0, CutStringLen-2)+"...";
						else
							name = l.Description;

						//s = s + "<div id='date_detal'><font color='#1672da'>" + l.StartHour + " " + l.EndHour + "</font><br />" + name + "</div>";
						s = s + "<div id='event_detail'>" + name + "</div>";

						i++;
					}
					//if (result.Count > DisplayEventNum)
					//    s = s + "<div id='date_detal'><font color='#1672da'>......</font></div>";

					break;
			}
			return s;
		}

		protected string ClueTip(string D)
		{
			if (string.IsNullOrWhiteSpace(D))
			{
				return "href='javascript:void(0)'";
			}
			else
			{
				string S = Convert.ToDateTime(D).Month.ToString() + "月" + Convert.ToDateTime(D).Day.ToString() + "日";
				return "class ='cluetip'   href='DayEvent.aspx?d=" + D + "' rel='DayEvent.aspx?d=" + D + "' title='" + S + "' ";
			}
		}

		protected void YearSel_Display(DateTime DateSelected)
		{

			int YearSelItemNum = 5;// 选中日期前后五年
			string OptionItem = "<select id='Year_Sel' onchange='Event_Sel();'>";
			for (int i = YearSelItemNum; i > 0; i--)
			{
				string YearItem = DateSelected.AddYears(-i).ToString("yyyy");
				OptionItem = OptionItem + "<option value='" + YearItem + "'>" + YearItem + "</option>";
			}
			for (int j = 0; j <= YearSelItemNum; j++)
			{
				string YearItem = DateSelected.AddYears(+j).ToString("yyyy");
				if (j == 0)
					OptionItem = OptionItem + "<option value='" + YearItem + "' selected='selected'>" + YearItem + "</option>";
				else
					OptionItem = OptionItem + "<option value='" + YearItem + "'>" + YearItem + "</option>";

			}
			OptionItem = OptionItem + "</select>";
			this.YearSelectDisplay = OptionItem;
		}
	}
}